package com.cyufeng.blog.service;

import com.cyufeng.blog.dto.SensitiveDto;
import com.cyufeng.blog.entity.Sensitive;
import com.cyufeng.blog.repository.SensitiveRepository;
import com.cyufeng.blog.service.mapper.SensitiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SensitiveService {
    @Autowired
    SensitiveRepository sensitiveRepository;

    SensitiveMapper sensitiveMapper = new SensitiveMapper();

    /**
     * 得到所有的敏感词
     * @return
     */
    public Set<String> getAllWord(){
        Set<String> words = sensitiveRepository.getAllWord();
        return words;
    }

    /**
     * 保存敏感词
     * @return
     */
    @Transactional
    public String saveSensitive(String word) {
        try {
            Sensitive sensitive = new Sensitive();
            sensitive.setWord(word);
            sensitive.setMadeTime(new Date());
            sensitive.setDr(0);
            sensitiveRepository.save(sensitive);
            return "success";
        } catch (Exception e) {
            return "error";
        }


    }
    /**
     * 分页查询文章，有条件-用于后台操作
     * @param pageNum
     * @param pageLimit
     * @return
     */
    public Map<String, Object> getAllSensitive(Integer pageNum, Integer pageLimit) {
        Pageable pageable = new PageRequest(pageNum - 1, pageLimit);

        Page<Sensitive> pp = sensitiveRepository.getAllSensitive_page(pageable);

//        EntityToDto
        List<Sensitive> sensitiveList = pp.getContent();
        List<SensitiveDto> articleDtoList = sensitiveMapper.entityTodto(sensitiveList);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("content", articleDtoList);//数据
        pageMap.put("totalPages", pp.getTotalPages());//一共有几页
        pageMap.put("totalElements", pp.getTotalElements());//一共有多少条数据
        pageMap.put("pageSize", pp.getPageable().getPageSize());//一页有几条数据
        pageMap.put("pageNumber", pp.getPageable().getPageNumber());//这是第几页
        return pageMap;
    }

    /**
     * 修改是否启用--用于后台操作
     * @param isEnable
     * @param id
     * @return
     */
    @Transactional
    public String setIsEnbale(Boolean isEnable, int id) {
        int tempIsEnable = 0;
        try {
            if (isEnable == true)
                tempIsEnable = 1;
            sensitiveRepository.setIsEnable(tempIsEnable, id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    //    修改编辑内容
    @Transactional
    public String saveEdit(String newWord, int id) {
        try {
            sensitiveRepository.saveEdit(newWord, id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }

    //    删除敏感词
    @Transactional
    public String deleteWord(int id) {
        try {
            java.util.Date utilDate=new Date();
            java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
            sensitiveRepository.deleteWord(sqlDate,id);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }
//    //    新增敏感词
//    @Transactional
//    public String saveWord(String newWord) {
//        try {
//            Sensitive sensitive = new Sensitive();
//            sen
//            sensitiveRepository.saveEdit(passwd, id);
//            return "success";
//        } catch (Exception e) {
//            return "error";
//        }
//    }
}
